package cn.itcast.ssh.web.action;

import cn.itcast.ssh.domain.FormTemplate;
import cn.itcast.ssh.service.FormTemplateService;
import cn.itcast.ssh.service.ProcessDefinitionService;
import cn.itcast.ssh.utils.ValueContext;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.struts2.ServletActionContext;
import top.jfunc.common.datetime.DatetimeUtils;
import top.jfunc.common.utils.CommonUtil;
import top.jfunc.common.utils.FileUtil;

import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author xiongshiyan at 2021/3/16 , contact me with email yanshixiong@126.com or phone 15208384257
 */
public class FormTemplateAction extends BaseAction<FormTemplate>{
    private FormTemplateService formTemplateService;
    private ProcessDefinitionService processDefinitionService;

    private File upload;
    private String uploadFileName;

    private InputStream inputStream;
    /**
     * 列表，显示所有表单模板
     */
    public String list(){
        List<FormTemplate> formTemplateList = formTemplateService.findAll();
        ValueContext.putValueContext("formTemplateList", formTemplateList);
        return "list";
    }
    /**
     * 删除，指定id的表单模板并且删除对应的文件
     */
    public String delete(){
        formTemplateService.deleteById(model.getId());
        return "toList";
    }
    /**
     * 添加页面
     */
    public String addUI(){
        List<ProcessDefinition> processDefinitionList = processDefinitionService.findAllLatestVersions();
        ValueContext.putValueContext("processDefinitionList", processDefinitionList);
        return "saveUI";
    }
    /**
     * 添加
     */
    public String add() throws IOException{
        String path = saveUploadFile(upload);

        model.setPath(path);
        formTemplateService.save(model);
        return "toList";
    }

    /**
     * 编辑页面
     */
    public String editUI(){
        List<ProcessDefinition> processDefinitionList = processDefinitionService.findAllLatestVersions();
        ValueContext.putValueContext("processDefinitionList", processDefinitionList);

        FormTemplate formTemplate = formTemplateService.getById(model.getId());
        ValueContext.putValueStack(formTemplate);

        return "saveUI";
    }
    /**
     * 编辑
     */
    public String edit() throws IOException{
        FormTemplate formTemplate = formTemplateService.getById(model.getId());
        formTemplate.setName(model.getName());
        formTemplate.setProcessDefinitionKey(model.getProcessDefinitionKey());
        if(null != upload){

            File file = new File(formTemplate.getPath());
            if (file.exists()) {
                file.delete();
            }

            String s = saveUploadFile(upload);
            formTemplate.setPath(s);
        }
        formTemplateService.update(formTemplate);
        return "toList";
    }
    /**
     * 下载表单的模板文件
     */
    public String download() throws Exception {
        FormTemplate formTemplate = formTemplateService.getById(model.getId());
        inputStream = new FileInputStream(formTemplate.getPath());
        String fileName = URLEncoder.encode(formTemplate.getName(), "utf-8");
        ValueContext.putValueContext("fileName", fileName);
        return "download";
    }

    public FormTemplateService getFormTemplateService() {
        return formTemplateService;
    }

    public void setFormTemplateService(FormTemplateService formTemplateService) {
        this.formTemplateService = formTemplateService;
    }

    public ProcessDefinitionService getProcessDefinitionService() {
        return processDefinitionService;
    }

    public void setProcessDefinitionService(ProcessDefinitionService processDefinitionService) {
        this.processDefinitionService = processDefinitionService;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
